<!DOCTYPE html>
<html lang="zh-CN">
<head>
  <meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=2">
<meta name="theme-color" content="#222">
<meta name="generator" content="Hexo 5.4.0">
  <link rel="apple-touch-icon" sizes="180x180" href="/images/apple-touch-icon-next.png">
  <link rel="icon" type="image/png" sizes="32x32" href="/images/astronaut32.png">
  <link rel="icon" type="image/png" sizes="16x16" href="/images/astronaut16.png">
  <link rel="mask-icon" href="/images/logo.svg" color="#222">

<link rel="stylesheet" href="/css/main.css">

<link rel="stylesheet" href="fonts.googleapis.com/css?family=EB Garamond:300,300italic,400,400italic,700,700italic|Cinzel Decorative:300,300italic,400,400italic,700,700italic|Noto Serif SC:300,300italic,400,400italic,700,700italic|Source Code Pro:300,300italic,400,400italic,700,700italic&display=swap&subset=latin,latin-ext">
<link rel="stylesheet" href="/lib/font-awesome/css/all.min.css">
  <link rel="stylesheet" href="//cdn.jsdelivr.net/gh/fancyapps/fancybox@3/dist/jquery.fancybox.min.css">
  <link rel="stylesheet" href="/lib/pace/pace-theme-pace-theme-minimal.min.css">
  <script src="/lib/pace/pace.min.js"></script>

<script id="hexo-configurations">
    var NexT = window.NexT || {};
    var CONFIG = {"hostname":"fat_fat_where_are_you.gitee.io","root":"/","scheme":"Gemini","version":"7.8.0","exturl":false,"sidebar":{"position":"left","display":"post","padding":18,"offset":12,"onmobile":false},"copycode":{"enable":true,"show_result":true,"style":"mac"},"back2top":{"enable":false,"sidebar":false,"scrollpercent":false},"bookmark":{"enable":false,"color":"#222","save":"auto"},"fancybox":true,"mediumzoom":false,"lazyload":true,"pangu":false,"comments":{"style":"tabs","active":null,"storage":true,"lazyload":false,"nav":null},"algolia":{"hits":{"per_page":10},"labels":{"input_placeholder":"Search for Posts","hits_empty":"We didn't find any results for the search: ${query}","hits_stats":"${hits} results found in ${time} ms"}},"localsearch":{"enable":true,"trigger":"auto","top_n_per_article":1,"unescape":false,"preload":false},"motion":{"enable":true,"async":false,"transition":{"post_block":"fadeIn","post_header":"slideDownIn","post_body":"slideDownIn","coll_header":"slideLeftIn","sidebar":"slideUpIn"}},"path":"search.xml"};
  </script>

<link href="https://fonts.googleapis.com/css2?family=Noto+Serif+SC:wght@500&display=swap" rel="stylesheet">
  <meta name="description" content="JavaScript WebAPI学习5">
<meta property="og:type" content="article">
<meta property="og:title" content="JavaScript 之 WebAPI 学习 5">
<meta property="og:url" content="http://fat_fat_where_are_you.gitee.io/archives/32bb900e.html">
<meta property="og:site_name" content="肥肉啊肥肉你在哪">
<meta property="og:description" content="JavaScript WebAPI学习5">
<meta property="og:locale" content="zh_CN">
<meta property="og:image" content="https://img-blog.csdnimg.cn/2020053121484679.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3dlaXhpbl80NjE3MDAzNA==,size_16,color_FFFFFF,t_70#pic_center">
<meta property="og:image" content="https://img-blog.csdnimg.cn/2020060208263282.gif#pic_center">
<meta property="og:image" content="https://img-blog.csdnimg.cn/20200602082656722.gif#pic_center">
<meta property="og:image" content="https://img-blog.csdnimg.cn/20200601185422964.gif#pic_center">
<meta property="og:image" content="https://img-blog.csdnimg.cn/20200601184614761.gif#pic_center">
<meta property="article:published_time" content="2020-08-11T00:07:44.000Z">
<meta property="article:modified_time" content="2020-08-11T00:07:44.000Z">
<meta property="article:author" content="肥肉啊肥肉你在哪">
<meta property="article:tag" content="JavaScript">
<meta name="twitter:card" content="summary">
<meta name="twitter:image" content="https://img-blog.csdnimg.cn/2020053121484679.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3dlaXhpbl80NjE3MDAzNA==,size_16,color_FFFFFF,t_70#pic_center">

<link rel="canonical" href="http://fat_fat_where_are_you.gitee.io/archives/32bb900e.html">


<script id="page-configurations">
  // https://hexo.io/docs/variables.html
  CONFIG.page = {
    sidebar: "",
    isHome : false,
    isPost : true,
    lang   : 'zh-CN'
  };
</script>
<link rel="stylesheet" type="text/css" href="/css/injector/main.css"><link rel="preload" as="style" href="/css/injector/light.css"><link rel="preload" as="style" href="/css/injector/dark.css">
  <title>JavaScript 之 WebAPI 学习 5 | 肥肉啊肥肉你在哪</title>
  






  <noscript>
  <style>
  .use-motion .brand,
  .use-motion .menu-item,
  .sidebar-inner,
  .use-motion .post-block,
  .use-motion .pagination,
  .use-motion .comments,
  .use-motion .post-header,
  .use-motion .post-body,
  .use-motion .collection-header { opacity: initial; }

  .use-motion .site-title,
  .use-motion .site-subtitle {
    opacity: initial;
    top: initial;
  }

  .use-motion .logo-line-before i { left: initial; }
  .use-motion .logo-line-after i { right: initial; }
  </style>
</noscript>

  <script src="https://cdn.jsdelivr.net/npm/jquery/dist/jquery.min.js"></script>
  <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/font-awesome/css/font-awesome.min.css">

</head>

<body itemscope itemtype="http://schema.org/WebPage">
  <div class="container use-motion">
    <div class="headband"></div>

    <header class="header" itemscope itemtype="http://schema.org/WPHeader">
      <div class="header-inner"><div class="site-brand-container">
  <div class="site-nav-toggle">
    <div class="toggle" aria-label="切换导航栏">
      <span class="toggle-line toggle-line-first"></span>
      <span class="toggle-line toggle-line-middle"></span>
      <span class="toggle-line toggle-line-last"></span>
    </div>
  </div>

  <div class="site-meta">

    <a href="/" class="brand" rel="start">
      <span class="logo-line-before"><i></i></span>
      <h1 class="site-title">肥肉啊肥肉你在哪</h1>
      <span class="logo-line-after"><i></i></span>
    </a>
  </div>

  <div class="site-nav-right">
    <div class="toggle popup-trigger">
        <i class="fa fa-search fa-fw fa-lg"></i>
    </div>
  </div>
</div>




<nav class="site-nav">
  <ul id="menu" class="main-menu menu">
        <li class="menu-item menu-item-home">

    <a href="/" rel="section"><i class="fa fa-home fa-fw"></i>首页</a>

  </li>
        <li class="menu-item menu-item-tags">

    <a href="/tags/" rel="section"><i class="fa fa-tags fa-fw"></i>标签</a>

  </li>
        <li class="menu-item menu-item-categories">

    <a href="/categories/" rel="section"><i class="fa fa-th fa-fw"></i>分类</a>

  </li>
        <li class="menu-item menu-item-archives">

    <a href="/archives/" rel="section"><i class="fa fa-archive fa-fw"></i>归档</a>

  </li>
        <li class="menu-item menu-item-movies">

    <a href="/movies/" rel="section"><i class="fa fa-film fa-fw"></i>观影</a>

  </li>
        <li class="menu-item menu-item-books">

    <a href="/books/" rel="section"><i class="fa fa-book fa-fw"></i>阅读</a>

  </li>
      <li class="menu-item menu-item-search">
        <a role="button" class="popup-trigger"><i class="fa fa-search fa-fw"></i>搜索
        </a>
      </li>
  </ul>
</nav>



  <div class="search-pop-overlay">
    <div class="popup search-popup">
        <div class="search-header">
  <span class="search-icon">
    <i class="fa fa-search"></i>
  </span>
  <div class="search-input-container">
    <input autocomplete="off" autocapitalize="off" placeholder="搜索..." spellcheck="false" type="search" class="search-input">
  </div>
  <span class="popup-btn-close">
    <i class="fa fa-times-circle"></i>
  </span>
</div>
<div id="search-result">
  <div id="no-result">
    <i class="fa fa-spinner fa-pulse fa-5x fa-fw"></i>
  </div>
</div>

    </div>
  </div>

</div>
    </header>

    
  <div class="reading-progress-bar"></div>


    <main class="main">
      <div class="main-inner">
        <div class="content-wrap">
          

          <div class="content post posts-expand">
            

    
  
  
  <article itemscope itemtype="http://schema.org/Article" class="post-block" lang="zh-CN">
    <link itemprop="mainEntityOfPage" href="http://fat_fat_where_are_you.gitee.io/archives/32bb900e.html">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="image" content="/images/touxiang.gif">
      <meta itemprop="name" content="肥肉啊肥肉你在哪">
      <meta itemprop="description" content>
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="肥肉啊肥肉你在哪">
    </span>
      <header class="post-header">
        <h1 class="post-title" itemprop="name headline">
          JavaScript 之 WebAPI 学习 5
        </h1>

        <div class="post-meta">
            <span class="post-meta-item">
              <span class="post-meta-item-icon">
                <i class="far fa-calendar"></i>
              </span>
              <span class="post-meta-item-text">发表于</span>

              <time title="创建时间：2020-08-11 08:07:44" itemprop="dateCreated datePublished" datetime="2020-08-11T08:07:44+08:00">2020-08-11</time>
            </span>
            <span class="post-meta-item">
              <span class="post-meta-item-icon">
                <i class="far fa-folder"></i>
              </span>
              <span class="post-meta-item-text">分类于</span>
                <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
                  <a href="/categories/%E5%89%8D%E7%AB%AF/" itemprop="url" rel="index"><span itemprop="name">前端</span></a>
                </span>
            </span>

          
          
          <br>
            <span id="/archives/32bb900e.html" class="post-meta-item leancloud_visitors" data-flag-title="JavaScript 之 WebAPI 学习 5" title="阅读次数">
              <span class="post-meta-item-icon">
                <i class="fa fa-eye"></i>
              </span>
              <span class="post-meta-item-text">阅读次数：</span>
              <span class="leancloud-visitors-count"></span>
            </span>
  
  <span class="post-meta-item">
    
      <span class="post-meta-item-icon">
        <i class="far fa-comment"></i>
      </span>
      <span class="post-meta-item-text">Valine：</span>
    
    <a title="valine" href="/archives/32bb900e.html#valine-comments" itemprop="discussionUrl">
      <span class="post-comments-count valine-comment-count" data-xid="/archives/32bb900e.html" itemprop="commentCount"></span>
    </a>
  </span>
  
  <br>
            <span class="post-meta-item" title="本文字数">
              <span class="post-meta-item-icon">
                <i class="far fa-file-word"></i>
              </span>
                <span class="post-meta-item-text">本文字数：</span>
              <span>15k</span>
            </span>
            <span class="post-meta-item" title="阅读时长">
              <span class="post-meta-item-icon">
                <i class="far fa-clock"></i>
              </span>
                <span class="post-meta-item-text">阅读时长 &asymp;</span>
              <span>14 分钟</span>
            </span>

        </div>
      </header>

    
    
    
    <div class="post-body" itemprop="articleBody">

      
        <h1 id="JavaScript-WebAPI 学习 5"><a href="#JavaScript-WebAPI 学习 5" class="headerlink" title="JavaScript WebAPI 学习 5"></a>JavaScript WebAPI 学习 5</h1><span id="more"></span>

<h2 id="元素偏移量 -offset- 系列"><a href="# 元素偏移量 -offset- 系列" class="headerlink" title="元素偏移量 offset 系列"></a>元素偏移量 offset 系列</h2><ul>
<li><p>offset 可以得到元素得位置，大小等</p>
<ul>
<li>获得元素距离带有定位的父元素位置</li>
<li>获得元素自身的宽度高度</li>
</ul>
</li>
<li><p>注意：返回的数值不带单位</p>
</li>
</ul>
<h3 id="offset 常用属性"><a href="#offset 常用属性" class="headerlink" title="offset 常用属性"></a>offset 常用属性</h3><table>
<thead>
<tr>
<th>location 对象的方法</th>
<th>返回值</th>
</tr>
</thead>
<tbody><tr>
<td>element.offsetParent</td>
<td>返回带有定位的父级元素，如果父级都没有定位则返回 body</td>
</tr>
<tr>
<td>element.offsetTop</td>
<td>返回带有定位父元素的上方的距离</td>
</tr>
<tr>
<td>element.offsetLeft</td>
<td>返回带有定位的父元素的左侧的距离</td>
</tr>
<tr>
<td>element.offsetWidth</td>
<td>返回盒子的宽度 (包括 padding，border) 返回值不含单位</td>
</tr>
<tr>
<td>element.offsetHeight</td>
<td>返回盒子的高度 (包括 padding，border) 返回值不含单位</td>
</tr>
</tbody></table>
<ul>
<li><code>element.offsetParent</code>和 <code>element.parentNode</code> 的区别：offsetParent 是返回带有定位的父亲，否则返回 body，而 parentNode 返回的是最近一级的父亲，无论有没有定位</li>
</ul>
<h4 id="offset- 与 -style- 的区别"><a href="#offset- 与 -style- 的区别" class="headerlink" title="offset 与 style 的区别"></a>offset 与 style 的区别</h4><ul>
<li>offset 与 style 都能获取元素的宽高等属性</li>
<li>style 只能获取行内样式的值 (写在标签内的属性)</li>
<li>但是 offset 可以得到任意样式表中的值</li>
<li>style 返回的有单位，offset 返回值没有单位</li>
</ul>
<p><img data-src="https://img-blog.csdnimg.cn/2020053121484679.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3dlaXhpbl80NjE3MDAzNA==,size_16,color_FFFFFF,t_70#pic_center" alt="在这里插入图片描述"></p>
<ul>
<li>重要！offset 是一个只读属性，只能获取不能赋值</li>
<li>style 是一个可读写属性，可以获取也可以赋值</li>
</ul>
<h4 id="offset- 案例：获取鼠标在盒子内的坐标"><a href="#offset- 案例：获取鼠标在盒子内的坐标" class="headerlink" title="offset 案例：获取鼠标在盒子内的坐标"></a>offset 案例：获取鼠标在盒子内的坐标</h4><ul>
<li><p>实现效果：在盒子内点击后返回鼠标距离盒子边缘的距离</p>
</li>
<li><p>思路：通过 <code>e.pageX</code> 获取鼠标在页面的横坐标，然后在减去 <code>offsetLeft</code> 就是鼠标在盒子中的坐标</p>
<ul>
<li>代码示例：</li>
</ul>
</li>
</ul>
<figure class="highlight html"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br></pre></td><td class="code"><pre><span class="line"> <span class="tag">&lt;<span class="name">style</span>&gt;</span><span class="css"></span></span><br><span class="line"><span class="css">    <span class="selector-class">.box</span> &#123;</span></span><br><span class="line"><span class="css">      <span class="attribute">height</span>: <span class="number">200px</span>;</span></span><br><span class="line"><span class="css">      <span class="attribute">width</span>: <span class="number">200px</span>;</span></span><br><span class="line"><span class="css">      <span class="attribute">background-color</span>: pink;</span></span><br><span class="line"><span class="css">    &#125;</span></span><br><span class="line"><span class="css">    </span><span class="tag">&lt;/<span class="name">style</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="name">head</span>&gt;</span></span><br><span class="line"></span><br><span class="line"><span class="tag">&lt;<span class="name">body</span>&gt;</span></span><br><span class="line">  <span class="tag">&lt;<span class="name">div</span> <span class="attr">class</span>=<span class="string">&quot;box&quot;</span>&gt;</span><span class="tag">&lt;/<span class="name">div</span>&gt;</span></span><br><span class="line">  <span class="tag">&lt;<span class="name">script</span>&gt;</span><span class="javascript"></span></span><br><span class="line"><span class="javascript">    <span class="keyword">var</span> box = <span class="built_in">document</span>.querySelector(<span class="string">&#x27;.box&#x27;</span>);</span></span><br><span class="line"><span class="javascript">    box.addEventListener(<span class="string">&#x27;mousemove&#x27;</span>, <span class="function"><span class="keyword">function</span> (<span class="params">e</span>) </span>&#123;</span></span><br><span class="line"><span class="javascript">      <span class="comment">// 这里的 box. 完全可以替换成 this，this 指向函数调用者</span></span></span><br><span class="line"><span class="javascript">      <span class="built_in">console</span>.log(e.pageX - box.offsetLeft);</span></span><br><span class="line"><span class="javascript">      <span class="built_in">console</span>.log(e.pageY - box.offsetTop);</span></span><br><span class="line"><span class="javascript">    &#125;)</span></span><br><span class="line"><span class="javascript">  </span><span class="tag">&lt;/<span class="name">script</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="name">body</span>&gt;</span></span><br></pre></td></tr></table></figure>

<h4 id="offset 实例：拖动登录框"><a href="#offset 实例：拖动登录框" class="headerlink" title="offset 实例：拖动登录框"></a>offset 实例：拖动登录框</h4><ul>
<li><p>实现效果：点击登录键后，出现一个小框里面是登录界面，然后这个界面可以拖动<br><img data-src="https://img-blog.csdnimg.cn/2020060208263282.gif#pic_center" alt="在这里插入图片描述"></p>
</li>
<li><p>实现思路：出现和隐藏比较简单，只需要绑定两个点击事件，然后点击时候 <code>display: none;</code> 就可以了</p>
</li>
<li><p>拖拽的思路：先有一个鼠标按下的事件 (mousedown) 和鼠标移动的事件 (mousemove)，然后把鼠标在网页中的坐标减去鼠标在盒子中的坐标然后就可以获得盒子移动的坐标</p>
<ul>
<li>因为代码实在太长了，这里只给出 js 的代码</li>
</ul>
</li>
</ul>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// 1. 获取元素</span></span><br><span class="line"><span class="keyword">var</span> login = <span class="built_in">document</span>.querySelector(<span class="string">&#x27;.login&#x27;</span>);</span><br><span class="line"><span class="keyword">var</span> mask = <span class="built_in">document</span>.querySelector(<span class="string">&#x27;.login-bg&#x27;</span>);</span><br><span class="line"><span class="keyword">var</span> link = <span class="built_in">document</span>.querySelector(<span class="string">&#x27;#link&#x27;</span>);</span><br><span class="line"><span class="keyword">var</span> closeBtn = <span class="built_in">document</span>.querySelector(<span class="string">&#x27;#closeBtn&#x27;</span>);</span><br><span class="line"><span class="keyword">var</span> title = <span class="built_in">document</span>.querySelector(<span class="string">&#x27;#title&#x27;</span>);</span><br><span class="line"><span class="comment">// 2. 点击弹出层这个链接 link  让 mask 和 login 显示出来</span></span><br><span class="line">link.addEventListener(<span class="string">&#x27;click&#x27;</span>, <span class="function"><span class="keyword">function</span> (<span class="params"></span>) </span>&#123;</span><br><span class="line">    mask.style.display = <span class="string">&#x27;block&#x27;</span>;</span><br><span class="line">    login.style.display = <span class="string">&#x27;block&#x27;</span>;</span><br><span class="line">&#125;)</span><br><span class="line"><span class="comment">// 3. 点击 closeBtn 就隐藏 mask 和 login</span></span><br><span class="line">closeBtn.addEventListener(<span class="string">&#x27;click&#x27;</span>, <span class="function"><span class="keyword">function</span> (<span class="params"></span>) </span>&#123;</span><br><span class="line">    mask.style.display = <span class="string">&#x27;none&#x27;</span>;</span><br><span class="line">    login.style.display = <span class="string">&#x27;none&#x27;</span>;</span><br><span class="line">&#125;)</span><br><span class="line"><span class="comment">// 4. 开始拖拽</span></span><br><span class="line"><span class="comment">// (1) 当我们鼠标按下， 就获得鼠标在盒子内的坐标</span></span><br><span class="line">title.addEventListener(<span class="string">&#x27;mousedown&#x27;</span>, <span class="function"><span class="keyword">function</span> (<span class="params">e</span>) </span>&#123;</span><br><span class="line">    <span class="keyword">var</span> x = e.pageX - login.offsetLeft;</span><br><span class="line">    <span class="keyword">var</span> y = e.pageY - login.offsetTop;</span><br><span class="line">    <span class="comment">// (2) 鼠标移动的时候，把鼠标在页面中的坐标，减去 鼠标在盒子内的坐标就是模态框的 left 和 top 值</span></span><br><span class="line">    <span class="built_in">document</span>.addEventListener(<span class="string">&#x27;mousemove&#x27;</span>, move)</span><br><span class="line"></span><br><span class="line">    <span class="function"><span class="keyword">function</span> <span class="title">move</span>(<span class="params">e</span>) </span>&#123;</span><br><span class="line">        login.style.left = e.pageX - x + <span class="string">&#x27;px&#x27;</span>;</span><br><span class="line">        login.style.top = e.pageY - y + <span class="string">&#x27;px&#x27;</span>;</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="comment">// (3) 鼠标弹起，就让鼠标移动事件移除</span></span><br><span class="line">    <span class="built_in">document</span>.addEventListener(<span class="string">&#x27;mouseup&#x27;</span>, <span class="function"><span class="keyword">function</span> (<span class="params"></span>) </span>&#123;</span><br><span class="line">        <span class="built_in">document</span>.removeEventListener(<span class="string">&#x27;mousemove&#x27;</span>, move);</span><br><span class="line">    &#125;)</span><br><span class="line">&#125;)</span><br></pre></td></tr></table></figure>

<ul>
<li>这里还要讲一下我学习时的误区，我当时想鼠标松开时解除 mousedown 事件不就好了，然而这里我就进入了一个误区，在鼠标按下的时候，mousemove 事件就已经被注册了，当我们的鼠标一移动，就不断的运行 mousemove 事件，也就是整个对话框和你的鼠标走，这时再去关闭 mousedown 事件已经没有作用了，因为对话框和你的鼠标走是在 mousemove 事件中运行的，所以我们在鼠标抬起的时候停止 mousemove 事件</li>
</ul>
<h3 id="案例：查看商品的放大镜效果"><a href="# 案例：查看商品的放大镜效果" class="headerlink" title="案例：查看商品的放大镜效果"></a>案例：查看商品的放大镜效果</h3><ul>
<li><p>在放在图片上的时候会出现大图来查看细节<br><img data-src="https://img-blog.csdnimg.cn/20200602082656722.gif#pic_center" alt="在这里插入图片描述"></p>
</li>
<li><p>实现方法：</p>
</li>
<li><p>鼠标经过图片，黄色遮挡层和大图片盒子显示，离开时隐藏这两个盒子</p>
</li>
<li><p>黄色遮挡层跟随鼠标的中央位置</p>
<ul>
<li>注意遮挡层应该把鼠标在盒子内的坐标赋给遮挡层的 left，top 值</li>
<li>操作的时候一定不要忘了单位！</li>
<li>移动的时候遮盖层不能超过父盒子的边缘，这里只需要判断他减完的值不要小于 0，假如小于 0 的话，那就让他的 left 值等于 0 就可以了</li>
</ul>
</li>
<li><p>移动黄色的遮挡层，大图片跟随移动</p>
</li>
<li><p>代码示例：</p>
<ul>
<li>test.html</li>
</ul>
</li>
</ul>
<figure class="highlight html"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br></pre></td><td class="code"><pre><span class="line"><span class="tag">&lt;<span class="name">style</span>&gt;</span><span class="css"></span></span><br><span class="line"><span class="css">    <span class="selector-class">.goods_img</span> &#123;</span></span><br><span class="line"><span class="css">        <span class="attribute">height</span>: <span class="number">400px</span>;</span></span><br><span class="line"><span class="css">        <span class="attribute">width</span>: <span class="number">400px</span>;</span></span><br><span class="line"><span class="css">        <span class="attribute">border</span>: <span class="number">1px</span> solid black;</span></span><br><span class="line"><span class="css">        <span class="attribute">cursor</span>: move;</span></span><br><span class="line"><span class="css">    &#125;</span></span><br><span class="line"><span class="css"></span></span><br><span class="line"><span class="css">    <span class="selector-class">.shelter</span> &#123;</span></span><br><span class="line"><span class="css">        <span class="attribute">display</span>: none;</span></span><br><span class="line"><span class="css">        <span class="attribute">position</span>: absolute;</span></span><br><span class="line"><span class="css">        <span class="attribute">top</span>: <span class="number">0</span>;</span></span><br><span class="line"><span class="css">        <span class="attribute">left</span>: <span class="number">0</span>;</span></span><br><span class="line"><span class="css">        <span class="attribute">height</span>: <span class="number">100px</span>;</span></span><br><span class="line"><span class="css">        <span class="attribute">width</span>: <span class="number">100px</span>;</span></span><br><span class="line"><span class="css">        <span class="attribute">pointer-events</span>: none;</span></span><br><span class="line"><span class="css">        <span class="attribute">background-color</span>: <span class="built_in">rgb</span>(<span class="number">247</span>, <span class="number">243</span>, <span class="number">21</span>, .<span class="number">5</span>);</span></span><br><span class="line"><span class="css">    &#125;</span></span><br><span class="line"><span class="css"></span></span><br><span class="line"><span class="css">    <span class="selector-class">.big</span> &#123;</span></span><br><span class="line"><span class="css">        <span class="attribute">display</span>: none;</span></span><br><span class="line"><span class="css">        <span class="attribute">position</span>: absolute;</span></span><br><span class="line"><span class="css">        <span class="attribute">left</span>: <span class="number">410px</span>;</span></span><br><span class="line"><span class="css">        <span class="attribute">top</span>: <span class="number">0px</span>;</span></span><br><span class="line"><span class="css">        <span class="attribute">width</span>: <span class="number">500px</span>;</span></span><br><span class="line"><span class="css">        <span class="attribute">height</span>: <span class="number">500px</span>;</span></span><br><span class="line"><span class="css">        <span class="attribute">background-color</span>: <span class="built_in">rgb</span>(<span class="number">5</span>, <span class="number">5</span>, <span class="number">5</span>);</span></span><br><span class="line"><span class="css">        <span class="attribute">overflow</span>: hidden;</span></span><br><span class="line"><span class="css">    &#125;</span></span><br><span class="line"><span class="css"></span></span><br><span class="line"><span class="css">    <span class="selector-class">.big</span> <span class="selector-tag">img</span> &#123;</span></span><br><span class="line"><span class="css">        <span class="attribute">position</span>: absolute;</span></span><br><span class="line"><span class="css">        <span class="attribute">height</span>: <span class="number">800px</span>;</span></span><br><span class="line"><span class="css">        <span class="attribute">width</span>: <span class="number">800px</span>;</span></span><br><span class="line"><span class="css">    &#125;</span></span><br><span class="line"><span class="css"></span><span class="tag">&lt;/<span class="name">style</span>&gt;</span></span><br><span class="line"></span><br><span class="line"><span class="tag">&lt;<span class="name">body</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">img</span> <span class="attr">src</span>=<span class="string">&quot;images/b3.png&quot;</span> <span class="attr">alt</span>=<span class="string">&quot;&quot;</span> <span class="attr">class</span>=<span class="string">&quot;goods_img&quot;</span>&gt;</span></span><br><span class="line"></span><br><span class="line">    <span class="tag">&lt;<span class="name">div</span> <span class="attr">class</span>=<span class="string">&quot;shelter&quot;</span>&gt;</span><span class="tag">&lt;/<span class="name">div</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">div</span> <span class="attr">class</span>=<span class="string">&quot;big&quot;</span>&gt;</span><span class="tag">&lt;<span class="name">img</span> <span class="attr">src</span>=<span class="string">&quot;images/big.jpg&quot;</span> <span class="attr">alt</span>=<span class="string">&quot;&quot;</span>&gt;</span><span class="tag">&lt;/<span class="name">div</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="name">body</span>&gt;</span></span><br></pre></td></tr></table></figure>

<ul>
<li>   detail.js</li>
</ul>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br></pre></td><td class="code"><pre><span class="line"><span class="built_in">window</span>.addEventListener(<span class="string">&#x27;load&#x27;</span>, <span class="function"><span class="keyword">function</span> (<span class="params"></span>) </span>&#123;</span><br><span class="line">    <span class="comment">// 获取一堆元素</span></span><br><span class="line">    <span class="keyword">var</span> goods_img = <span class="built_in">this</span>.document.querySelector(<span class="string">&#x27;.goods_img&#x27;</span>);</span><br><span class="line">    <span class="keyword">var</span> big = <span class="built_in">this</span>.document.querySelector(<span class="string">&#x27;.big&#x27;</span>);</span><br><span class="line">    <span class="keyword">var</span> shelter = <span class="built_in">this</span>.document.querySelector(<span class="string">&#x27;.shelter&#x27;</span>);</span><br><span class="line">    <span class="keyword">var</span> big_img = big.querySelector(<span class="string">&#x27;img&#x27;</span>);</span><br><span class="line">    <span class="comment">// 鼠标经过小图片时，显示遮挡层和大图片</span></span><br><span class="line">    goods_img.addEventListener(<span class="string">&#x27;mouseover&#x27;</span>, <span class="function"><span class="keyword">function</span> (<span class="params"></span>) </span>&#123;</span><br><span class="line">        shelter.style.display = <span class="string">&#x27;block&#x27;</span>;</span><br><span class="line">        big.style.display = <span class="string">&#x27;block&#x27;</span>;</span><br><span class="line">    &#125;)</span><br><span class="line">    <span class="comment">// 鼠标离开时，隐藏遮挡层和大图片</span></span><br><span class="line">    goods_img.addEventListener(<span class="string">&#x27;mouseout&#x27;</span>, <span class="function"><span class="keyword">function</span> (<span class="params"></span>) </span>&#123;</span><br><span class="line">        shelter.style.display = <span class="string">&#x27;none&#x27;</span>;</span><br><span class="line">        big.style.display = <span class="string">&#x27;none&#x27;</span>;</span><br><span class="line">    &#125;)</span><br><span class="line">    <span class="comment">// 当鼠标移动时，开始获取鼠标的横纵坐标进行计算</span></span><br><span class="line">    goods_img.addEventListener(<span class="string">&#x27;mousemove&#x27;</span>, <span class="function"><span class="keyword">function</span> (<span class="params">m</span>) </span>&#123;</span><br><span class="line">        <span class="comment">// 在盒子内的坐标</span></span><br><span class="line">        <span class="keyword">var</span> x = m.pageX - goods_img.offsetLeft;</span><br><span class="line">        <span class="keyword">var</span> y = m.pageY - goods_img.offsetTop;</span><br><span class="line">        <span class="comment">// 遮挡层在在盒子内的 top 和 left 值</span></span><br><span class="line">        <span class="keyword">var</span> maskX = x - shelter.offsetWidth / <span class="number">2</span>;</span><br><span class="line">        <span class="keyword">var</span> maskY = y - shelter.offsetWidth / <span class="number">2</span>;</span><br><span class="line">        <span class="comment">// 遮挡层移动的最大距离, 因为商品盒子是正方形，所以两个是相等的</span></span><br><span class="line">        <span class="keyword">var</span> maskMax = goods_img.offsetWidth - shelter.offsetWidth;</span><br><span class="line">        <span class="comment">// 判断盒子不能超出图片边界</span></span><br><span class="line">        <span class="keyword">if</span> (maskX &lt;= <span class="number">0</span>) &#123;</span><br><span class="line">            maskX = <span class="number">0</span>;</span><br><span class="line">        &#125; <span class="keyword">else</span> <span class="keyword">if</span> (maskX &gt;= maskMax) &#123;</span><br><span class="line">            maskX = maskMax;</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="keyword">if</span> (maskY &lt;= <span class="number">0</span>) &#123;</span><br><span class="line">            maskY = <span class="number">0</span>;</span><br><span class="line">        &#125; <span class="keyword">else</span> <span class="keyword">if</span> (maskY &gt;= maskMax) &#123;</span><br><span class="line">            maskY = maskMax;</span><br><span class="line">        &#125;</span><br><span class="line"></span><br><span class="line">        shelter.style.left = maskX + <span class="string">&#x27;px&#x27;</span>;</span><br><span class="line">        shelter.style.top = maskY + <span class="string">&#x27;px&#x27;</span>;</span><br><span class="line">        <span class="keyword">var</span> bigMax = big_img.offsetWidth - big.offsetWidth;</span><br><span class="line">        <span class="comment">// 这个是按照比例来显示右侧的图片的</span></span><br><span class="line">        <span class="keyword">var</span> bigX = maskX * bigMax / maskMax;</span><br><span class="line">        <span class="keyword">var</span> bigY = maskY * bigMax / maskMax;</span><br><span class="line">        <span class="comment">// 盒子不动，图片要往下往左走，所以这里为负值</span></span><br><span class="line">        big_img.style.left = -bigX + <span class="string">&#x27;px&#x27;</span>;</span><br><span class="line">        big_img.style.top = -bigY + <span class="string">&#x27;px&#x27;</span>;</span><br><span class="line">    &#125;)</span><br><span class="line">&#125;)</span><br></pre></td></tr></table></figure>

<ul>
<li><p>用到的小知识：</p>
<ul>
<li>js 写在外部的做法：</li>
<li>js 写在外部时需要等待所有页面加载完成才能执行 js 操作，所以 window 要加上一个 load 事件</li>
<li>js 引入时 <code>&lt;script src=&quot;detail.js&quot;&gt;&lt;/script&gt;</code> 这样引入就可以</li>
</ul>
</li>
<li><p>写这段代码时所遇到的问题：</p>
<ul>
<li>当在商品图片里移动鼠标时，遮罩层会一闪一闪的，这是因为鼠标到商品图片上上以后会执行 mouseover 事件，出现遮罩层后会因为没有在商品图片上而执行 mouseout 事件，所以就会出现一闪一闪的状况</li>
<li>解决方法：在遮罩层上面加 <code>pointer-events: none;</code> 属性，这个属性的意思是鼠标穿过有这个属性的元素，鼠标事件指向他下面的元素</li>
<li>还有还有，一定不要忘记加单位</li>
</ul>
</li>
</ul>
<h2 id="元素可视区 -client- 系列"><a href="# 元素可视区 -client- 系列" class="headerlink" title="元素可视区 client 系列"></a>元素可视区 client 系列</h2><ul>
<li>获取元素可视区的相关信息，例如元素边框大小，元素大小等</li>
</ul>
<table>
<thead>
<tr>
<th>client 对象的属性</th>
<th>返回值</th>
</tr>
</thead>
<tbody><tr>
<td>element.clientTop</td>
<td>返回元素上边框大小</td>
</tr>
<tr>
<td>element.clientLeft</td>
<td>返回元素左边框大小</td>
</tr>
<tr>
<td>element.clientWidth</td>
<td>返回盒子的宽度 (包括 padding，不含边框，返回值不含单位)</td>
</tr>
<tr>
<td>element.clientHeight</td>
<td>返回盒子的高度 (包括 padding，不含边框，返回值不含单位)</td>
</tr>
</tbody></table>
<ul>
<li>和 offsetWidth 的区别：clientWidth 不包含边框，offsetWidth 包含边框</li>
</ul>
<h2 id="立即执行函数"><a href="# 立即执行函数" class="headerlink" title="立即执行函数"></a>立即执行函数</h2><ul>
<li>   立即执行函数：不需要调用，立马自己能够执行的函数</li>
<li>   一共有两种写法：</li>
</ul>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// 1.</span></span><br><span class="line">(<span class="function"><span class="keyword">function</span>(<span class="params"></span>) </span>&#123;&#125;)()</span><br><span class="line"><span class="comment">// 2.</span></span><br><span class="line">(<span class="function"><span class="keyword">function</span>(<span class="params"></span>) </span>&#123;&#125;())</span><br><span class="line"><span class="comment">// 第二个小括号相当于调用函数 (可以传参)</span></span><br></pre></td></tr></table></figure>

<ul>
<li>立即执行函数最大的作用是：独立创建了一个作用域 (和外界的作用域隔开，里面都是局部变量)</li>
</ul>
<blockquote>
<p>惊，这个莫非是命名空间</p>
</blockquote>
<h3 id="读淘宝 -flexible-js- 代码"><a href="# 读淘宝 -flexible-js- 代码" class="headerlink" title="读淘宝 flexible.js 代码"></a>读淘宝 flexible.js 代码</h3><ul>
<li><p>看淘宝写的代码真的赏心悦目，思考的极为周全，真的应该好好阅读大厂写的代码</p>
</li>
<li><p>大家可以看看 <a target="_blank" rel="external nofollow noopener noreferrer" href="https://gitee.com/xiaoqiang001/jsapis_material/blob/master/%E7%AC%AC%E4%BA%94%E5%A4%A9/07-flexible%E5%88%86%E6%9E%90.js"> 老师写注释的源码</a></p>
</li>
<li><p><code>pageshow</code> 事件：页面重新加载触发的事件</p>
</li>
<li><p>和 load 事件的区别：</p>
</li>
<li><p>下面三种情况都能触发 load 事件：</p>
<ul>
<li>a 标签的超链接</li>
<li>F5 或者刷新按钮</li>
<li>前进后退按钮</li>
</ul>
</li>
<li><p>火狐中有一个特例叫往返缓存：缓存不止缓存了页面数据，同时缓存了 DOM 和 JavaScript 的状态，实际上是把整个缓存都保存到了页面里，这时的上面的按钮就不能触发 load 事件</p>
</li>
<li><p>这个时候用 pageshow 事件就能很好的解决这个问题</p>
</li>
<li><p>而且 pageshow 有一个属性，<code>persisted</code>: 这个属性如果是从缓存加载的话那就返回 true</p>
</li>
</ul>
<h2 id="元素滚动 -scroll- 系列"><a href="# 元素滚动 -scroll- 系列" class="headerlink" title="元素滚动 scroll 系列"></a>元素滚动 scroll 系列</h2><ul>
<li>scroll 可以动态获取元素的大小，滚动的距离 (经常获取滚动条)</li>
</ul>
<table>
<thead>
<tr>
<th>scroll 对象的属性</th>
<th>返回值</th>
</tr>
</thead>
<tbody><tr>
<td>element.scrollTop</td>
<td>返回被滚动上去的距离 (不含单位)</td>
</tr>
<tr>
<td>element.scrollLeft</td>
<td>返回被滚动到左边的距离 (不含单位)</td>
</tr>
<tr>
<td>element.scrollWidth</td>
<td>返回内容的宽度 (包括 padding，不含边框，返回值不含单位)</td>
</tr>
<tr>
<td>element.scrollHeight</td>
<td>返回内容的高度 (包括 padding，不含边框，返回值不含单位)</td>
</tr>
</tbody></table>
<ul>
<li><p>scrollWidth 和 clientWidth 的区别：scrollWidth 假如内容超出边框的话返回的是内容的宽度，而 clientWidth 假如超过边框的话仍然返回的是盒子的宽度</p>
</li>
<li><p><code>onscroll</code> 事件：当拖动滚动条时触发</p>
</li>
</ul>
<h3 id="scroll- 案例：仿淘宝固定右侧侧边栏"><a href="#scroll- 案例：仿淘宝固定右侧侧边栏" class="headerlink" title="scroll 案例：仿淘宝固定右侧侧边栏"></a>scroll 案例：仿淘宝固定右侧侧边栏</h3><ul>
<li>   实现效果：淘宝右侧有一个导航栏，一旦超过某个位置他就会变成固定定位 (无论页面如何滚动他都固定在那里)</li>
<li>   小知识：<code>window.pageYOffset</code>获取页面被滚动到上面的距离 (左右滚动是 pageXOffset)</li>
<li>这个属性有兼容性问题，IE9 以上支持</li>
<li>   代码示例：</li>
</ul>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">var</span> sliderbar = <span class="built_in">document</span>.querySelector(<span class="string">&#x27;.slider-bar&#x27;</span>);</span><br><span class="line"><span class="keyword">var</span> banner = <span class="built_in">document</span>.querySelector(<span class="string">&#x27;.banner&#x27;</span>);</span><br><span class="line"><span class="comment">// 页面滚动事件</span></span><br><span class="line"><span class="keyword">var</span> bannerTop = banner.offsetTop;</span><br><span class="line"><span class="keyword">var</span> sliderbarTop = sliderbar.offsetTop - bannerTop;</span><br><span class="line"><span class="built_in">document</span>.addEventListener(<span class="string">&#x27;scroll&#x27;</span>, <span class="function"><span class="keyword">function</span> (<span class="params"></span>) </span>&#123;</span><br><span class="line">  <span class="comment">// 如果他滚动到了 banner 这个模块，就变成固定定位，并且更改一下 top 的值</span></span><br><span class="line">    <span class="keyword">if</span> (<span class="built_in">window</span>.pageYOffset &gt;= bannerTop) &#123;</span><br><span class="line">        sliderbar.style.position = <span class="string">&#x27;fixed&#x27;</span>;</span><br><span class="line">        sliderbar.style.top = sliderbarTop + <span class="string">&#x27;px&#x27;</span>;</span><br><span class="line">    &#125; <span class="keyword">else</span> &#123;</span><br><span class="line">        sliderbar.style.position = <span class="string">&#x27;absolute&#x27;</span>;</span><br><span class="line">        sliderbar.style.top = <span class="string">&#x27;300px&#x27;</span>;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;)</span><br></pre></td></tr></table></figure>

<h3 id="三个系列的小结"><a href="# 三个系列的小结" class="headerlink" title="三个系列的小结"></a>三个系列的小结</h3><ul>
<li>这三个系列大小的对比</li>
</ul>
<table>
<thead>
<tr>
<th>三个系列大小的对比</th>
<th>返回值</th>
</tr>
</thead>
<tbody><tr>
<td>element.offsetWidth</td>
<td>返回元素 padding，边框，内容的宽度 返回值没有单位</td>
</tr>
<tr>
<td>element.clientWidth</td>
<td>返回元素 padding，内容的宽度 返回值没有单位</td>
</tr>
<tr>
<td>element.scrollWidth</td>
<td>返回元素 padding，内容的宽度，假如内容超过边框返回内容实际的宽度</td>
</tr>
</tbody></table>
<ul>
<li>offset 获取元素位置：offsetLeft</li>
<li>client 获取元素大小：clientWidth</li>
<li>scroll 获取滚动距离：scrollTop</li>
</ul>
<h2 id="mouseenter- 和 -mouseover- 的区别"><a href="#mouseenter- 和 -mouseover- 的区别" class="headerlink" title="mouseenter 和 mouseover 的区别"></a>mouseenter 和 mouseover 的区别</h2><ul>
<li><p>mouseover 经过自身盒子会触发，经过子盒子也会触发</p>
</li>
<li><p>而 mouseenter 只会经过自身盒子触发</p>
</li>
<li><p>产生这两种区别的原因是 mouseenter 不会冒泡</p>
</li>
<li><p>同样，mouseleave 在鼠标离开时也不会冒泡</p>
</li>
</ul>
<h2 id="动画函数封装"><a href="# 动画函数封装" class="headerlink" title="动画函数封装"></a>动画函数封装</h2><ul>
<li>   动画原理：通过定时器 setInterval() 不断移动盒子位置</li>
<li>   代码示例：有一个有定位的小盒子，一直向右移动</li>
</ul>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">var</span> div = <span class="built_in">document</span>.querySelector(<span class="string">&#x27;div&#x27;</span>);</span><br><span class="line"><span class="keyword">var</span> timer = <span class="built_in">setInterval</span>(<span class="function"><span class="keyword">function</span> (<span class="params"></span>) </span>&#123;</span><br><span class="line">    <span class="keyword">if</span> (div.offsetLeft &gt; <span class="number">800</span>) &#123;</span><br><span class="line">        <span class="built_in">clearInterval</span>(timer);</span><br><span class="line">    &#125;</span><br><span class="line">    div.style.left = div.offsetLeft + <span class="number">5</span> + <span class="string">&#x27;px&#x27;</span>;</span><br><span class="line">&#125;, <span class="number">30</span>)</span><br></pre></td></tr></table></figure>

<ul>
<li>中间遇到的问题：<ul>
<li>div 的 offsetLeft 属性是只读的，所以要用 style 来改变元素 left 值</li>
</ul>
</li>
</ul>
<h3 id="动画函数的封装"><a href="# 动画函数的封装" class="headerlink" title="动画函数的封装"></a>动画函数的封装</h3><ul>
<li>   写一个函数来封装这个动画</li>
</ul>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// obj: 要进行动画的元素 target: 目标位置</span></span><br><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">animate</span>(<span class="params">obj,target</span>) </span>&#123;</span><br><span class="line">    <span class="keyword">var</span> timer = <span class="built_in">setInterval</span>(<span class="function"><span class="keyword">function</span> (<span class="params"></span>) </span>&#123;</span><br><span class="line">    <span class="keyword">if</span> (obj.offsetLeft &gt; target) &#123;</span><br><span class="line">        <span class="built_in">clearInterval</span>(timer);</span><br><span class="line">    &#125;</span><br><span class="line">    obj.style.left = obj.offsetLeft + <span class="number">5</span> + <span class="string">&#x27;px&#x27;</span>;</span><br><span class="line">&#125;, <span class="number">30</span>)</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<ul>
<li><p>上面那个函数的优化：把 timer 作为我们引入元素的一个属性</p>
<ul>
<li>这样做的好处：不在用 var 开辟内存空间，通过元素可以找到每一个定时器的 timer</li>
</ul>
</li>
<li><p>这里有一个小 bug ，当我们是点击一个按钮才让盒子走时，当我们不断的点击按钮，盒子移动的速度会越来越快</p>
<ul>
<li><p>解决方法：让我们的元素只有一个定时器</p>
</li>
<li><p>先清除所有的定时器，保证只有一个定时器执行</p>
</li>
</ul>
</li>
</ul>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// obj: 要进行动画的元素 target: 目标位置</span></span><br><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">animate</span>(<span class="params">obj,target</span>) </span>&#123;</span><br><span class="line">  <span class="built_in">clearInterval</span>(obj.timer);</span><br><span class="line">    obj.timer = <span class="built_in">setInterval</span>(<span class="function"><span class="keyword">function</span> (<span class="params"></span>) </span>&#123;</span><br><span class="line">    <span class="keyword">if</span> (obj.offsetLeft &gt; target) &#123;</span><br><span class="line">        <span class="built_in">clearInterval</span>(obj.timer);</span><br><span class="line">    &#125;</span><br><span class="line">    obj.style.left = obj.offsetLeft + <span class="number">5</span> + <span class="string">&#x27;px&#x27;</span>;</span><br><span class="line">&#125;, <span class="number">30</span>)</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h3 id="缓动动画原理"><a href="# 缓动动画原理" class="headerlink" title="缓动动画原理"></a>缓动动画原理</h3><ul>
<li><p>缓动动画：动画是缓慢停下来的</p>
</li>
<li><p>原理：每次移动的距离慢慢的变小，使用公式：(目标值 - 现在的位置) / 10</p>
<ul>
<li><p>公式里的 10 可以更换为任意值，值越小步长越大</p>
</li>
<li><p>代码示例：</p>
</li>
</ul>
</li>
</ul>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br></pre></td><td class="code"><pre><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">animate</span>(<span class="params">obj, target, callback</span>) </span>&#123;</span><br><span class="line">    <span class="comment">// console.log(callback);  callback = function() &#123;&#125;  调用的时候 callback()</span></span><br><span class="line"></span><br><span class="line">    <span class="comment">// 先清除以前的定时器，只保留当前的一个定时器执行</span></span><br><span class="line">    <span class="built_in">clearInterval</span>(obj.timer);</span><br><span class="line">    obj.timer = <span class="built_in">setInterval</span>(<span class="function"><span class="keyword">function</span> (<span class="params"></span>) </span>&#123;</span><br><span class="line">        <span class="comment">// 步长值写到定时器的里面</span></span><br><span class="line">        <span class="comment">// 把我们步长值改为整数 不要出现小数的问题</span></span><br><span class="line">        <span class="comment">// var step = Math.ceil((target - obj.offsetLeft) / 10);</span></span><br><span class="line">        <span class="keyword">var</span> step = (target - obj.offsetLeft) / <span class="number">10</span>;</span><br><span class="line">        step = step &gt; <span class="number">0</span> ? <span class="built_in">Math</span>.ceil(step) : <span class="built_in">Math</span>.floor(step);</span><br><span class="line">        <span class="keyword">if</span> (obj.offsetLeft == target) &#123;</span><br><span class="line">            <span class="comment">// 停止动画 本质是停止定时器</span></span><br><span class="line">            <span class="built_in">clearInterval</span>(obj.timer);</span><br><span class="line">            <span class="comment">// 回调函数写到定时器结束里面</span></span><br><span class="line">            <span class="comment">// if (callback) &#123;</span></span><br><span class="line">            <span class="comment">//     // 调用函数</span></span><br><span class="line">            <span class="comment">//     callback();</span></span><br><span class="line">            <span class="comment">// &#125;</span></span><br><span class="line">            callback &amp;&amp; callback();</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="comment">// 把每次加 1 这个步长值改为一个慢慢变小的值  步长公式：(目标值 - 现在的位置) / 10</span></span><br><span class="line">        obj.style.left = obj.offsetLeft + step + <span class="string">&#x27;px&#x27;</span>;</span><br><span class="line"></span><br><span class="line">    &#125;, <span class="number">15</span>);</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<ul>
<li>这里出现了小数精度的问题，盒子停下来时他其实和目标值差零点几</li>
<li>我们可以把步长取整，而且是向上取整 (为了防止盒子会倒退几个像素) 这样就不会出现差一点的问题</li>
<li>但是这里又出现了一个小问题：当我们从一个远往近了移动时，发现和目标值差很多<ul>
<li>这是因为从远往近移动是负值，所以这时候应该向下取整</li>
<li>下面加一个判断，当移动的距离是正值时向上取整，当移动距离为负时向下取整</li>
</ul>
</li>
</ul>
<h3 id="动画函数单独封装到 -js- 文件内"><a href="# 动画函数单独封装到 -js- 文件内" class="headerlink" title="动画函数单独封装到 js 文件内"></a>动画函数单独封装到 js 文件内</h3><ul>
<li>可以把常用的动画封装成一个文件</li>
<li>使用的时候：<code>&lt;script src=&quot;XXX.js&quot;&gt;&lt;/script&gt;</code>引入</li>
</ul>
<h3 id="网页轮播图"><a href="# 网页轮播图" class="headerlink" title="网页轮播图"></a>网页轮播图</h3><ul>
<li>终于做到了心心念念的网页轮播图了</li>
</ul>
<p><img data-src="https://img-blog.csdnimg.cn/20200601185422964.gif#pic_center" alt="在这里插入图片描述"></p>
<ul>
<li>一共有五个功能：</li>
<li>鼠标经过轮播图时，显示左右按钮，离开时隐藏按钮</li>
<li>点击左右按钮时图片往左或右播放一张</li>
<li>播放的同时下面的小圆圈也同时变化</li>
<li>鼠标经过轮播图会暂停播放图片</li>
</ul>
<h4 id="显示 - 隐藏左右按钮"><a href="# 显示 - 隐藏左右按钮" class="headerlink" title="显示 / 隐藏左右按钮"></a>显示 / 隐藏左右按钮</h4><ul>
<li>   鼠标经过轮播图的盒子显示这两个按钮，离开时隐藏</li>
<li>   代码示例：</li>
</ul>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// 鼠标经过显示左右按钮</span></span><br><span class="line">focus.addEventListener(<span class="string">&#x27;mouseenter&#x27;</span>, <span class="function"><span class="keyword">function</span> (<span class="params"></span>) </span>&#123;</span><br><span class="line">    arrow_l.style.display = <span class="string">&#x27;block&#x27;</span>;</span><br><span class="line">    arrow_r.style.display = <span class="string">&#x27;block&#x27;</span>;</span><br><span class="line">&#125;)</span><br><span class="line">focus.addEventListener(<span class="string">&#x27;mouseleave&#x27;</span>, <span class="function"><span class="keyword">function</span> (<span class="params"></span>) </span>&#123;</span><br><span class="line">    arrow_l.style.display = <span class="string">&#x27;none&#x27;</span>;</span><br><span class="line">    arrow_r.style.display = <span class="string">&#x27;none&#x27;</span>;</span><br><span class="line">&#125;)</span><br></pre></td></tr></table></figure>

<h4 id="动态生成小圆圈"><a href="# 动态生成小圆圈" class="headerlink" title="动态生成小圆圈"></a>动态生成小圆圈</h4><ul>
<li>   根据图片的个数动态生成有多少个小圆圈</li>
<li>   代码示例：</li>
</ul>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// 动态生成小圆圈</span></span><br><span class="line"><span class="keyword">var</span> ul = focus.querySelector(<span class="string">&#x27;ul&#x27;</span>);</span><br><span class="line"><span class="keyword">var</span> ol = focus.querySelector(<span class="string">&#x27;ol&#x27;</span>);</span><br><span class="line"><span class="keyword">for</span> (<span class="keyword">var</span> i = <span class="number">0</span>; i &lt; ul.children.length; i++) &#123;</span><br><span class="line">    <span class="comment">// 创建 li</span></span><br><span class="line">    <span class="keyword">var</span> li = <span class="built_in">document</span>.createElement(<span class="string">&#x27;li&#x27;</span>);</span><br><span class="line">    <span class="comment">// 插入到 ol 里</span></span><br><span class="line">    ol.appendChild(li);</span><br><span class="line">&#125;</span><br><span class="line"><span class="comment">// 把 ol 的第一个 li 设置为选中状态</span></span><br><span class="line">ol.children[<span class="number">0</span>].className = <span class="string">&#x27;current&#x27;</span>;</span><br></pre></td></tr></table></figure>

<h5 id="单击小圆圈让小圆圈处于选中状态"><a href="# 单击小圆圈让小圆圈处于选中状态" class="headerlink" title="单击小圆圈让小圆圈处于选中状态"></a>单击小圆圈让小圆圈处于选中状态</h5><ul>
<li>   给每个小圆圈绑定点击事件，然后把其他所有的 li 去除 current 类，只给点击的这个元素 添加 current</li>
<li>   代码示例：</li>
</ul>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// 给每个元素绑定点击事件</span></span><br><span class="line">li.addEventListener(<span class="string">&#x27;click&#x27;</span>,<span class="function"><span class="keyword">function</span>(<span class="params"></span>)</span>&#123;</span><br><span class="line">    <span class="comment">// 把所有 li 去除 current 类名</span></span><br><span class="line">    <span class="keyword">for</span>(<span class="keyword">var</span> i = <span class="number">0</span>;i&lt;ol.children.length;i++) &#123;</span><br><span class="line">        ol.children[i].className = <span class="string">&#x27;&#x27;</span>;</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="comment">// 把点击的这个留下</span></span><br><span class="line">    <span class="built_in">this</span>.className = <span class="string">&#x27;current&#x27;</span>;</span><br><span class="line">&#125;)</span><br></pre></td></tr></table></figure>

<h4 id="点击小圆点实现图片滚动"><a href="# 点击小圆点实现图片滚动" class="headerlink" title="点击小圆点实现图片滚动"></a>点击小圆点实现图片滚动</h4><ul>
<li><p>图片滚动是通过移动 ul 来实现滚动效果</p>
</li>
<li><p>我们可以直接使用之前封装好的移动函数，注意引入文件时要写在 index.js 上面，和函数声明是一样的，要先声明在使用</p>
</li>
<li><p>滚动图片的方式：点击某个小圆圈，就让图片滚动索引号乘以图片的宽度作为 ul 移动的距离</p>
<ul>
<li>通过设置自定义属性来记录当前小圆圈的索引号</li>
</ul>
</li>
</ul>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// 给每个小圆圈记录索引号</span></span><br><span class="line">li.setAttribute(<span class="string">&#x27;index&#x27;</span>, i);</span><br></pre></td></tr></table></figure>

<ul>
<li>   这个事件还是在鼠标点击之后完成的，所以还写在 click 事件内</li>
</ul>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br></pre></td><td class="code"><pre><span class="line">li.addEventListener(<span class="string">&#x27;click&#x27;</span>, <span class="function"><span class="keyword">function</span> (<span class="params"></span>) </span>&#123;</span><br><span class="line"><span class="comment">// 把所有 li 去除 current 类名</span></span><br><span class="line"><span class="keyword">for</span> (<span class="keyword">var</span> i = <span class="number">0</span>; i &lt; ol.children.length; i++) &#123;</span><br><span class="line">    ol.children[i].className = <span class="string">&#x27;&#x27;</span>;</span><br><span class="line">&#125;</span><br><span class="line"><span class="comment">// 把点击的这个留下</span></span><br><span class="line"><span class="built_in">this</span>.className = <span class="string">&#x27;current&#x27;</span>;</span><br><span class="line"><span class="comment">// 点击图片实现图片滑动效果</span></span><br><span class="line"><span class="comment">// 点击某个 li 获取当前的索引号</span></span><br><span class="line"><span class="keyword">var</span> index = <span class="built_in">this</span>.getAttribute(<span class="string">&#x27;index&#x27;</span>);</span><br><span class="line"><span class="comment">// 移动的距离就是当前索引号乘以图片的宽度</span></span><br><span class="line">animate(ul, -index * focusWidth);</span><br><span class="line">&#125;)</span><br></pre></td></tr></table></figure>

<h4 id="点击右侧按钮实现图片滚动"><a href="# 点击右侧按钮实现图片滚动" class="headerlink" title="点击右侧按钮实现图片滚动"></a>点击右侧按钮实现图片滚动</h4><ul>
<li><p>声明一个变量 num 每点击一次，就让这个变量自增，再乘以图片的宽度，就是 ul 的移动距离</p>
<ul>
<li>无缝滚动的原理：在所有图片后面再放一个第一张图片，当从最后一张滚动到伪第一张后再迅速无动画的跳回真实的第一张</li>
</ul>
</li>
</ul>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// 点击右侧按钮，图片滚动一张</span></span><br><span class="line"><span class="keyword">var</span> num = <span class="number">0</span>;</span><br><span class="line">arrow_r.addEventListener(<span class="string">&#x27;click&#x27;</span>, <span class="function"><span class="keyword">function</span> (<span class="params"></span>) </span>&#123;</span><br><span class="line">    <span class="keyword">if</span> (num == ul.children.length - <span class="number">1</span>) &#123;</span><br><span class="line">        ul.style.left = <span class="number">0</span>;</span><br><span class="line">        num = <span class="number">0</span>;</span><br><span class="line">    &#125;</span><br><span class="line">    num++;</span><br><span class="line">    animate(ul, -num * focusWidth);</span><br><span class="line">&#125;)</span><br></pre></td></tr></table></figure>

<ul>
<li><p>但是使用这种方法就会出现一个 bug，他下面的小圆圈就会多一个</p>
</li>
<li><p>而且我们不想每增加一次图片就要在 html 里修改</p>
</li>
<li><p>下面是解决方法：</p>
</li>
<li><p>克隆 ul 里的第一个 li cloneNode() 里面参数为 true 是深克隆(复制里面的子节点)，false 浅克隆</p>
</li>
<li><p>并且添加到 ul 的最后，用 appendChild() 添加</p>
<ul>
<li>又因为这个 li 是在自动生成 li 之后添加的，所以不会多出一个圆点</li>
</ul>
</li>
</ul>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// 克隆第一张图片放到 ul 的最后面</span></span><br><span class="line"><span class="keyword">var</span> first = ul.children[<span class="number">0</span>].cloneNode(<span class="literal">true</span>);</span><br><span class="line">ul.appendChild(first);</span><br></pre></td></tr></table></figure>

<ul>
<li>   下面的小圆点也跟着上面的一起变化</li>
<li>   在声明一个专门控制小圆点的变量，然后他只要运行到了我们克隆的图片，就让他归零，注意一定要先加加后判断</li>
</ul>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br></pre></td><td class="code"><pre><span class="line">arrow_r.addEventListener(<span class="string">&#x27;click&#x27;</span>, <span class="function"><span class="keyword">function</span> (<span class="params"></span>) </span>&#123;</span><br><span class="line">    <span class="keyword">if</span> (num == ul.children.length - <span class="number">1</span>) &#123;</span><br><span class="line">        ul.style.left = <span class="number">0</span>;</span><br><span class="line">        num = <span class="number">0</span>;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    num++;</span><br><span class="line">    animate(ul, -num * focusWidth);</span><br><span class="line">    circle++;</span><br><span class="line">    <span class="keyword">if</span> (circle == ol.children.length) &#123;</span><br><span class="line">        circle = <span class="number">0</span>;</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="keyword">for</span> (<span class="keyword">var</span> i = <span class="number">0</span>; i &lt; ol.children.length; i++) &#123;</span><br><span class="line">        ol.children[i].className = <span class="string">&#x27;&#x27;</span>;</span><br><span class="line">    &#125;</span><br><span class="line">    ol.children[circle].className = <span class="string">&#x27;current&#x27;</span>;</span><br><span class="line">&#125;)</span><br></pre></td></tr></table></figure>

<h5 id="一点小 bug"><a href="# 一点小 bug" class="headerlink" title="一点小 bug"></a>一点小 bug</h5><ul>
<li>   在我们点击右移按钮在点击底下的小圆点时，就会出现不同步的情况</li>
<li>   我们要让两个模块的变量联系起来，让上面点击完小圆点之后也要告诉下面的右移模块我们现在到哪里了 (小圆点的变量也是一样的，也需要上面告诉他我们移动到了哪里)</li>
</ul>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// index 是指示点击小圆点时位置的变量</span></span><br><span class="line"><span class="comment">// num 和 circle 是指示右移箭头位置的变量和指示小圆点是哪个的变量</span></span><br><span class="line">num = index;</span><br><span class="line">circle = index;</span><br></pre></td></tr></table></figure>

<h4 id="左侧的按钮实现滚动"><a href="# 左侧的按钮实现滚动" class="headerlink" title="左侧的按钮实现滚动"></a>左侧的按钮实现滚动</h4><ul>
<li>   左侧按钮和右侧按钮的功能差不多</li>
<li>   像镜子一样把 arrow_r 翻转一下就可以了</li>
</ul>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br></pre></td><td class="code"><pre><span class="line">arrow_l.addEventListener(<span class="string">&#x27;click&#x27;</span>, <span class="function"><span class="keyword">function</span> (<span class="params"></span>) </span>&#123;</span><br><span class="line"><span class="keyword">if</span> (num == <span class="number">0</span>) &#123;</span><br><span class="line">    num = ul.children.length - <span class="number">1</span>;</span><br><span class="line">    ul.style.left = -num * focusWidth + <span class="string">&#x27;px&#x27;</span>;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line">num--;</span><br><span class="line">animate(ul, -num * focusWidth);</span><br><span class="line">circle--;</span><br><span class="line"><span class="keyword">if</span> (circle &lt; <span class="number">0</span>) &#123;</span><br><span class="line">    circle = ol.children.length - <span class="number">1</span>;</span><br><span class="line">&#125;</span><br><span class="line"><span class="keyword">for</span> (<span class="keyword">var</span> i = <span class="number">0</span>; i &lt; ol.children.length; i++) &#123;</span><br><span class="line">    ol.children[i].className = <span class="string">&#x27;&#x27;</span>;</span><br><span class="line">&#125;</span><br><span class="line">ol.children[circle].className = <span class="string">&#x27;current&#x27;</span>;</span><br><span class="line">&#125;);</span><br></pre></td></tr></table></figure>

<h4 id="轮播图的自动播放效果"><a href="# 轮播图的自动播放效果" class="headerlink" title="轮播图的自动播放效果"></a>轮播图的自动播放效果</h4><ul>
<li>   自动播放就相当于点了右侧的按钮</li>
<li>   一个超级强的事件：手动调用点击事件 <code>element.click()</code> 模拟人手点击这个按钮</li>
</ul>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">var</span> timer = <span class="built_in">setInterval</span>(<span class="function"><span class="keyword">function</span> (<span class="params"></span>) </span>&#123;</span><br><span class="line">    arrow_r.click();</span><br><span class="line">&#125;, <span class="number">2000</span>);</span><br></pre></td></tr></table></figure>

<ul>
<li>鼠标一经过时停止定时器也就停止滚动，鼠标一经过就开始定时器</li>
</ul>
<h4 id="节流阀"><a href="# 节流阀" class="headerlink" title="节流阀"></a>节流阀</h4><ul>
<li><p>节流阀：当上一个动画执行完毕时再去执行下一个函数动画</p>
</li>
<li><p>在这里我们当一个图片滚动完成后才能接着点击这个按钮</p>
</li>
<li><p>我们用回调函数来实现锁住函数和解锁函数</p>
</li>
<li><p>具体思路：</p>
<ul>
<li>我们在事件外面声明一个变量，当我们运行这个事件后的时候我们把这个变量置为 false，里面有一个判断条件，当变量为 false 时运行不了这个函数</li>
<li>我们再在动画运行的最后把这个变量重新置为 true 他就不会动画动的过快了</li>
</ul>
</li>
</ul>
<h3 id="案例：返回顶部动画版"><a href="# 案例：返回顶部动画版" class="headerlink" title="案例：返回顶部动画版"></a>案例：返回顶部动画版</h3><ul>
<li>窗口滚动到文档中的指定位置：<code>window.scroll(x,y)</code><ul>
<li>里面的 x 和 y 都是不加单位的</li>
</ul>
</li>
<li>但是这种方法出来的效果是直接回到顶部的，没有动画效果</li>
</ul>
<hr>
<ul>
<li>   这里我们可以直接修改我们的动画函数，把所有 left 修改为 top 就可以了</li>
</ul>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br></pre></td><td class="code"><pre><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">animate</span>(<span class="params">obj, target, callback</span>) </span>&#123;</span><br><span class="line"><span class="comment">// console.log(callback);  callback = function() &#123;&#125;  调用的时候 callback()</span></span><br><span class="line"></span><br><span class="line"><span class="comment">// 先清除以前的定时器，只保留当前的一个定时器执行</span></span><br><span class="line"><span class="built_in">clearInterval</span>(obj.timer);</span><br><span class="line">obj.timer = <span class="built_in">setInterval</span>(<span class="function"><span class="keyword">function</span> (<span class="params"></span>) </span>&#123;</span><br><span class="line">    <span class="comment">// 步长值写到定时器的里面</span></span><br><span class="line">    <span class="comment">// 把我们步长值改为整数 不要出现小数的问题</span></span><br><span class="line">    <span class="comment">// var step = Math.ceil((target - obj.offsetLeft) / 10);</span></span><br><span class="line">    <span class="keyword">var</span> step = (target - <span class="built_in">window</span>.pageYOffset) / <span class="number">10</span>;</span><br><span class="line">    step = step &gt; <span class="number">0</span> ? <span class="built_in">Math</span>.ceil(step) : <span class="built_in">Math</span>.floor(step);</span><br><span class="line">    <span class="keyword">if</span> (<span class="built_in">window</span>.pageYOffset == target) &#123;</span><br><span class="line">        <span class="comment">// 停止动画 本质是停止定时器</span></span><br><span class="line">        <span class="built_in">clearInterval</span>(obj.timer);</span><br><span class="line">        <span class="comment">// 回调函数写到定时器结束里面</span></span><br><span class="line">        <span class="comment">// if (callback) &#123;</span></span><br><span class="line">        <span class="comment">//     // 调用函数</span></span><br><span class="line">        <span class="comment">//     callback();</span></span><br><span class="line">        <span class="comment">// &#125;</span></span><br><span class="line">        callback &amp;&amp; callback();</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="comment">// 把每次加 1 这个步长值改为一个慢慢变小的值  步长公式：(目标值 - 现在的位置) / 10</span></span><br><span class="line">    <span class="comment">// obj.style.top = window.pageYOffset + step + &#x27;px&#x27;;</span></span><br><span class="line">    <span class="built_in">window</span>.scroll(<span class="number">0</span>, <span class="built_in">window</span>.pageYOffset + step);</span><br><span class="line">&#125;, <span class="number">15</span>);</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h3 id="案例：筋斗云"><a href="# 案例：筋斗云" class="headerlink" title="案例：筋斗云"></a>案例：筋斗云</h3><ul>
<li>实现样式：<ul>
<li>鼠标经过某个 li 筋斗云移动到当前 li 的位置</li>
<li>离开这个 li 时，筋斗云复原为原来的位置</li>
<li>点击了某个 li 筋斗云就会停留在这个 li 的位置</li>
</ul>
</li>
</ul>
<p><img data-src="https://img-blog.csdnimg.cn/20200601184614761.gif#pic_center" alt="在这里插入图片描述"></p>
<ul>
<li><p>具体思路：</p>
<ul>
<li><p>通过设置筋斗云的 offsetLeft 值实现筋斗云的移动</p>
</li>
<li><p>设置一个全局变量作为筋斗云的偏移量，然后绑定三个事件</p>
<ul>
<li>鼠标经过把筋斗云的位置移动到鼠标处</li>
<li>鼠标离开把上面设置的全局变量设置为筋斗云的偏移量</li>
<li>鼠标点击改变全局变量为现在的偏移量</li>
</ul>
</li>
<li><p>代码示例：</p>
</li>
</ul>
</li>
</ul>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// 1. 获取元素</span></span><br><span class="line"><span class="keyword">var</span> cloud = <span class="built_in">document</span>.querySelector(<span class="string">&#x27;.cloud&#x27;</span>);</span><br><span class="line"><span class="keyword">var</span> c_nav = <span class="built_in">document</span>.querySelector(<span class="string">&#x27;.c-nav&#x27;</span>);</span><br><span class="line"><span class="keyword">var</span> lis = c_nav.querySelectorAll(<span class="string">&#x27;li&#x27;</span>);</span><br><span class="line"><span class="comment">// 2. 给所有的小 li 绑定事件</span></span><br><span class="line"><span class="comment">// 这个 current 做为筋斗云的起始位置</span></span><br><span class="line"><span class="keyword">var</span> current = <span class="number">0</span>;</span><br><span class="line"><span class="keyword">for</span> (<span class="keyword">var</span> i = <span class="number">0</span>; i &lt; lis.length; i++) &#123;</span><br><span class="line">    <span class="comment">// (1) 鼠标经过把当前小 li 的位置做为目标值</span></span><br><span class="line">    lis[i].addEventListener(<span class="string">&#x27;mouseenter&#x27;</span>, <span class="function"><span class="keyword">function</span>(<span class="params"></span>) </span>&#123;</span><br><span class="line">        animate(cloud, <span class="built_in">this</span>.offsetLeft);</span><br><span class="line">    &#125;);</span><br><span class="line">    <span class="comment">// (2) 鼠标离开就回到起始的位置</span></span><br><span class="line">    lis[i].addEventListener(<span class="string">&#x27;mouseleave&#x27;</span>, <span class="function"><span class="keyword">function</span>(<span class="params"></span>) </span>&#123;</span><br><span class="line">        animate(cloud, current);</span><br><span class="line">    &#125;);</span><br><span class="line">    <span class="comment">// (3) 当我们鼠标点击，就把当前位置做为目标值</span></span><br><span class="line">    lis[i].addEventListener(<span class="string">&#x27;click&#x27;</span>, <span class="function"><span class="keyword">function</span>(<span class="params"></span>) </span>&#123;</span><br><span class="line">        current = <span class="built_in">this</span>.offsetLeft;</span><br><span class="line">    &#125;);</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<blockquote>
<p>本文转载自<a target="_blank" rel="external nofollow noopener noreferrer" href="https://blog.csdn.net/weixin_46170034/article/details/106462095">https://blog.csdn.net/weixin_46170034/article/details/106462095</a></p>
</blockquote>

    </div>

    
    
    
      
  <div class="popular-posts-header">相关文章推荐</div>
  <ul class="popular-posts">
    <li class="popular-posts-item">
      <div class="popular-posts-title"><a href="\archives\35d65417.html" rel="bookmark">JavaScript 之 WebAPI 学习 1</a></div>
    </li>
    <li class="popular-posts-item">
      <div class="popular-posts-title"><a href="\archives\dbd8353b.html" rel="bookmark">JavaScript 之 WebAPI 学习 3</a></div>
    </li>
    <li class="popular-posts-item">
      <div class="popular-posts-title"><a href="\archives\45bca098.html" rel="bookmark">JavaScript 之 WebAPI 学习 4</a></div>
    </li>
    <li class="popular-posts-item">
      <div class="popular-posts-title"><a href="\archives\5606f168.html" rel="bookmark">JavaScript 基础语法 1</a></div>
    </li>
    <li class="popular-posts-item">
      <div class="popular-posts-title"><a href="\archives\cf0fa0d2.html" rel="bookmark">JavaScript 基础语法 2</a></div>
    </li>
  </ul>

        <div class="reward-container">
  <div>感谢各位打赏的小伙伴</div>
  <button onclick="var qr = document.getElementById('qr'); qr.style.display = (qr.style.display === 'none') ? 'block' : 'none';">
    打赏
  </button>
  <div id="qr" style="display: none;">
      
      <div style="display: inline-block;">
        <img src="/images/wechatpay.png" alt="肥肉啊肥肉你在哪 微信支付">
        <p>微信支付</p>
      </div>
      
      <div style="display: inline-block;">
        <img src="/images/alipay.png" alt="肥肉啊肥肉你在哪 支付宝">
        <p>支付宝</p>
      </div>

  </div>
</div>

        

<div>
<ul class="post-copyright">
  <li class="post-copyright-author">
    <strong>本文作者： </strong>肥肉啊肥肉你在哪
  </li>
  <li class="post-copyright-link">
    <strong>本文链接：</strong>
    <a href="http://fat_fat_where_are_you.gitee.io/archives/32bb900e.html" title="JavaScript 之 WebAPI 学习 5">http://fat_fat_where_are_you.gitee.io/archives/32bb900e.html</a>
  </li>
  <li class="post-copyright-license">
    <strong>版权声明： </strong>本博客所有文章除特别声明外，均采用 <a href="https://creativecommons.org/licenses/by-nc-sa/4.0/" rel="external nofollow noopener noreferrer" target="_blank"><i class="fab fa-fw fa-creative-commons"></i>BY-NC-SA</a> 许可协议。转载请注明出处！
  </li>
</ul>
</div>


      <footer class="post-footer">
          <div class="post-tags">
              <a href="/tags/JavaScript/" rel="tag"><i class="fas fa-tags"></i> JavaScript</a>
          </div>

        
  <div class="post-widgets">
    <div class="wp_rating">
      <div id="wpac-rating"></div>
    </div>
  </div>


        
    <div class="post-nav">
      <div class="post-nav-item">
    <a href="/archives/45bca098.html" rel="prev" title="JavaScript 之 WebAPI 学习 4">
      <i class="fa fa-chevron-left"></i> JavaScript 之 WebAPI 学习 4
    </a></div>
      <div class="post-nav-item">
    <a href="/archives/629074ab.html" rel="next" title="三大分析法 --SWOT、PEST、波特五力">
      三大分析法 --SWOT、PEST、波特五力 <i class="fa fa-chevron-right"></i>
    </a></div>
    </div>
      </footer>
    
  </article>
  
  
  



          </div>
          
    <div class="comments" id="valine-comments"></div>

<script>
  window.addEventListener('tabs:register', () => {
    let { activeClass } = CONFIG.comments;
    if (CONFIG.comments.storage) {
      activeClass = localStorage.getItem('comments_active') || activeClass;
    }
    if (activeClass) {
      let activeTab = document.querySelector(`a[href="#comment-${activeClass}"]`);
      if (activeTab) {
        activeTab.click();
      }
    }
  });
  if (CONFIG.comments.storage) {
    window.addEventListener('tabs:click', event => {
      if (!event.target.matches('.tabs-comment .tab-content .tab-pane')) return;
      let commentClass = event.target.classList[1];
      localStorage.setItem('comments_active', commentClass);
    });
  }
</script>

        </div>
          
  
  <div class="toggle sidebar-toggle">
    <span class="toggle-line toggle-line-first"></span>
    <span class="toggle-line toggle-line-middle"></span>
    <span class="toggle-line toggle-line-last"></span>
  </div>

  <aside class="sidebar">
    <div class="sidebar-inner">

      <ul class="sidebar-nav motion-element">
        <li class="sidebar-nav-toc">
          文章目录
        </li>
        <li class="sidebar-nav-overview">
          站点概览
        </li>
      </ul>

      <!--noindex-->
      <div class="post-toc-wrap sidebar-panel">
          <div class="post-toc motion-element"><ol class="nav"><li class="nav-item nav-level-1"><a class="nav-link" href="#JavaScript-WebAPI%20%E5%AD%A6%E4%B9%A0%205"><span class="nav-text">JavaScript WebAPI 学习 5</span></a><ol class="nav-child"><li class="nav-item nav-level-2"><a class="nav-link" href="#%E5%85%83%E7%B4%A0%E5%81%8F%E7%A7%BB%E9%87%8F%20-offset-%20%E7%B3%BB%E5%88%97"><span class="nav-text">元素偏移量 offset 系列</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#offset%20%E5%B8%B8%E7%94%A8%E5%B1%9E%E6%80%A7"><span class="nav-text">offset 常用属性</span></a><ol class="nav-child"><li class="nav-item nav-level-4"><a class="nav-link" href="#offset-%20%E4%B8%8E%20-style-%20%E7%9A%84%E5%8C%BA%E5%88%AB"><span class="nav-text">offset 与 style 的区别</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#offset-%20%E6%A1%88%E4%BE%8B%EF%BC%9A%E8%8E%B7%E5%8F%96%E9%BC%A0%E6%A0%87%E5%9C%A8%E7%9B%92%E5%AD%90%E5%86%85%E7%9A%84%E5%9D%90%E6%A0%87"><span class="nav-text">offset 案例：获取鼠标在盒子内的坐标</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#offset%20%E5%AE%9E%E4%BE%8B%EF%BC%9A%E6%8B%96%E5%8A%A8%E7%99%BB%E5%BD%95%E6%A1%86"><span class="nav-text">offset 实例：拖动登录框</span></a></li></ol></li><li class="nav-item nav-level-3"><a class="nav-link" href="#%E6%A1%88%E4%BE%8B%EF%BC%9A%E6%9F%A5%E7%9C%8B%E5%95%86%E5%93%81%E7%9A%84%E6%94%BE%E5%A4%A7%E9%95%9C%E6%95%88%E6%9E%9C"><span class="nav-text">案例：查看商品的放大镜效果</span></a></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#%E5%85%83%E7%B4%A0%E5%8F%AF%E8%A7%86%E5%8C%BA%20-client-%20%E7%B3%BB%E5%88%97"><span class="nav-text">元素可视区 client 系列</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#%E7%AB%8B%E5%8D%B3%E6%89%A7%E8%A1%8C%E5%87%BD%E6%95%B0"><span class="nav-text">立即执行函数</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#%E8%AF%BB%E6%B7%98%E5%AE%9D%20-flexible-js-%20%E4%BB%A3%E7%A0%81"><span class="nav-text">读淘宝 flexible.js 代码</span></a></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#%E5%85%83%E7%B4%A0%E6%BB%9A%E5%8A%A8%20-scroll-%20%E7%B3%BB%E5%88%97"><span class="nav-text">元素滚动 scroll 系列</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#scroll-%20%E6%A1%88%E4%BE%8B%EF%BC%9A%E4%BB%BF%E6%B7%98%E5%AE%9D%E5%9B%BA%E5%AE%9A%E5%8F%B3%E4%BE%A7%E4%BE%A7%E8%BE%B9%E6%A0%8F"><span class="nav-text">scroll 案例：仿淘宝固定右侧侧边栏</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#%E4%B8%89%E4%B8%AA%E7%B3%BB%E5%88%97%E7%9A%84%E5%B0%8F%E7%BB%93"><span class="nav-text">三个系列的小结</span></a></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#mouseenter-%20%E5%92%8C%20-mouseover-%20%E7%9A%84%E5%8C%BA%E5%88%AB"><span class="nav-text">mouseenter 和 mouseover 的区别</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#%E5%8A%A8%E7%94%BB%E5%87%BD%E6%95%B0%E5%B0%81%E8%A3%85"><span class="nav-text">动画函数封装</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#%E5%8A%A8%E7%94%BB%E5%87%BD%E6%95%B0%E7%9A%84%E5%B0%81%E8%A3%85"><span class="nav-text">动画函数的封装</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#%E7%BC%93%E5%8A%A8%E5%8A%A8%E7%94%BB%E5%8E%9F%E7%90%86"><span class="nav-text">缓动动画原理</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#%E5%8A%A8%E7%94%BB%E5%87%BD%E6%95%B0%E5%8D%95%E7%8B%AC%E5%B0%81%E8%A3%85%E5%88%B0%20-js-%20%E6%96%87%E4%BB%B6%E5%86%85"><span class="nav-text">动画函数单独封装到 js 文件内</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#%E7%BD%91%E9%A1%B5%E8%BD%AE%E6%92%AD%E5%9B%BE"><span class="nav-text">网页轮播图</span></a><ol class="nav-child"><li class="nav-item nav-level-4"><a class="nav-link" href="#%E6%98%BE%E7%A4%BA%20-%20%E9%9A%90%E8%97%8F%E5%B7%A6%E5%8F%B3%E6%8C%89%E9%92%AE"><span class="nav-text">显示 &#x2F; 隐藏左右按钮</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#%E5%8A%A8%E6%80%81%E7%94%9F%E6%88%90%E5%B0%8F%E5%9C%86%E5%9C%88"><span class="nav-text">动态生成小圆圈</span></a><ol class="nav-child"><li class="nav-item nav-level-5"><a class="nav-link" href="#%E5%8D%95%E5%87%BB%E5%B0%8F%E5%9C%86%E5%9C%88%E8%AE%A9%E5%B0%8F%E5%9C%86%E5%9C%88%E5%A4%84%E4%BA%8E%E9%80%89%E4%B8%AD%E7%8A%B6%E6%80%81"><span class="nav-text">单击小圆圈让小圆圈处于选中状态</span></a></li></ol></li><li class="nav-item nav-level-4"><a class="nav-link" href="#%E7%82%B9%E5%87%BB%E5%B0%8F%E5%9C%86%E7%82%B9%E5%AE%9E%E7%8E%B0%E5%9B%BE%E7%89%87%E6%BB%9A%E5%8A%A8"><span class="nav-text">点击小圆点实现图片滚动</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#%E7%82%B9%E5%87%BB%E5%8F%B3%E4%BE%A7%E6%8C%89%E9%92%AE%E5%AE%9E%E7%8E%B0%E5%9B%BE%E7%89%87%E6%BB%9A%E5%8A%A8"><span class="nav-text">点击右侧按钮实现图片滚动</span></a><ol class="nav-child"><li class="nav-item nav-level-5"><a class="nav-link" href="#%E4%B8%80%E7%82%B9%E5%B0%8F%20bug"><span class="nav-text">一点小 bug</span></a></li></ol></li><li class="nav-item nav-level-4"><a class="nav-link" href="#%E5%B7%A6%E4%BE%A7%E7%9A%84%E6%8C%89%E9%92%AE%E5%AE%9E%E7%8E%B0%E6%BB%9A%E5%8A%A8"><span class="nav-text">左侧的按钮实现滚动</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#%E8%BD%AE%E6%92%AD%E5%9B%BE%E7%9A%84%E8%87%AA%E5%8A%A8%E6%92%AD%E6%94%BE%E6%95%88%E6%9E%9C"><span class="nav-text">轮播图的自动播放效果</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#%E8%8A%82%E6%B5%81%E9%98%80"><span class="nav-text">节流阀</span></a></li></ol></li><li class="nav-item nav-level-3"><a class="nav-link" href="#%E6%A1%88%E4%BE%8B%EF%BC%9A%E8%BF%94%E5%9B%9E%E9%A1%B6%E9%83%A8%E5%8A%A8%E7%94%BB%E7%89%88"><span class="nav-text">案例：返回顶部动画版</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#%E6%A1%88%E4%BE%8B%EF%BC%9A%E7%AD%8B%E6%96%97%E4%BA%91"><span class="nav-text">案例：筋斗云</span></a></li></ol></li></ol></li></ol></div>
      </div>
      <!--/noindex-->

      <div class="site-overview-wrap sidebar-panel">
        <div class="site-author motion-element" itemprop="author" itemscope itemtype="http://schema.org/Person">
    <img class="site-author-image" itemprop="image" alt="肥肉啊肥肉你在哪" src="/images/touxiang.gif">
  <p class="site-author-name" itemprop="name">肥肉啊肥肉你在哪</p>
  <div class="site-description" itemprop="description"></div>
</div>
<div class="site-state-wrap motion-element">
  <nav class="site-state">
      <div class="site-state-item site-state-posts">
          <a href="/archives/">
        
          <span class="site-state-item-count">38</span>
          <span class="site-state-item-name">日志</span>
        </a>
      </div>
      <div class="site-state-item site-state-categories">
            <a href="/categories/">
          
        <span class="site-state-item-count">4</span>
        <span class="site-state-item-name">分类</span></a>
      </div>
      <div class="site-state-item site-state-tags">
            <a href="/tags/">
          
        <span class="site-state-item-count">16</span>
        <span class="site-state-item-name">标签</span></a>
      </div>
  </nav>
</div>
  <div class="links-of-author motion-element">
      <span class="links-of-author-item">
        <a href="https://github.com/feirouafeirou" title="GitHub → https:&#x2F;&#x2F;github.com&#x2F;feirouafeirou" rel="external nofollow noopener noreferrer" target="_blank"><i class="fab fa-github fa-fw"></i>GitHub</a>
      </span>
      <span class="links-of-author-item">
        <a href="http://wpa.qq.com/msgrd?v=3&uin=1729013657&site=qq&menu=yes" title="QQ → http:&#x2F;&#x2F;wpa.qq.com&#x2F;msgrd?v&#x3D;3&amp;uin&#x3D;1729013657&amp;site&#x3D;qq&amp;menu&#x3D;yes" rel="external nofollow noopener noreferrer" target="_blank"><i class="fab fa-qq fa-fw"></i>QQ</a>
      </span>
  </div>


  <div class="links-of-blogroll motion-element">
    <div class="links-of-blogroll-title"><i class="fa fa-link fa-fw"></i>
      友情链接
    </div>
    <ul class="links-of-blogroll-list">
        <li class="links-of-blogroll-item">
          <a href="http://www.woshipm.com/" title="http:&#x2F;&#x2F;www.woshipm.com" rel="external nofollow noopener noreferrer" target="_blank">人人都是产品经理</a>
        </li>
        <li class="links-of-blogroll-item">
          <a href="http://www.chanpin100.com/" title="http:&#x2F;&#x2F;www.chanpin100.com" rel="external nofollow noopener noreferrer" target="_blank">产品壹佰</a>
        </li>
    </ul>
  </div>

      </div>

    </div>
  </aside>

  <div id="sidebar-dimmer"></div>


      </div>
    </main>

    <footer class="footer">
      <div class="footer-inner">
        

        

<div class="copyright">
  
  &copy; 2020 – 
  <span itemprop="copyrightYear">2022</span>
  <span class="with-love">
    <i class="fa fa-heart"></i>
  </span>
  <span class="author" itemprop="copyrightHolder">肥肉啊肥肉你在哪</span>
    <span class="post-meta-divider">|</span>
    <span class="post-meta-item-icon">
      <i class="fa fa-chart-area"></i>
    </span>
    <span title="站点总字数">237k</span>
    <span class="post-meta-divider">|</span>
    <span class="post-meta-item-icon">
      <i class="fa fa-coffee"></i>
    </span>
    <span title="站点阅读时长">3:35</span>
</div>

<div class="translate-style">
繁/简：<a id="translateLink" href="javascript:translatePage();">繁体
</a>
</div>
<script type="text/javascript" src="/js/tw_cn.js"></script>
<script type="text/javascript">
var defaultEncoding = 2; //网站编写字体是否繁体，1-繁体，2-简体
var translateDelay = 0; //延迟时间,若不在前, 要设定延迟翻译时间, 如100表示100ms,默认为0
var cookieDomain = "https://feirouafeirou.github.io/"; //Cookie地址, 一定要设定, 通常为你的网址
var msgToTraditionalChinese = "繁体"; //此处可以更改为你想要显示的文字
var msgToSimplifiedChinese = "简体"; //同上，但两处均不建议更改
var translateButtonId = "translateLink"; //默认互换id
translateInitilization();
</script>


        
<div class="busuanzi-count">
  <script async src="https://busuanzi.ibruce.info/busuanzi/2.3/busuanzi.pure.mini.js"></script>
    <span class="post-meta-item" id="busuanzi_container_site_uv" style="display: none;">
      <span class="post-meta-item-icon">
        <i class="fa fa-user"></i>
      </span>
      <span class="site-uv" title="总访客量">
        <span id="busuanzi_value_site_uv"></span>
      </span>
    </span>
    <span class="post-meta-divider">|</span>
    <span class="post-meta-item" id="busuanzi_container_site_pv" style="display: none;">
      <span class="post-meta-item-icon">
        <i class="fa fa-eye"></i>
      </span>
      <span class="site-pv" title="总访问量">
        <span id="busuanzi_value_site_pv"></span>
      </span>
    </span>
</div>








      </div>
    </footer>
  </div>

  
  <script src="/lib/anime.min.js"></script>
  <script src="//cdn.jsdelivr.net/npm/jquery@3/dist/jquery.min.js"></script>
  <script src="//cdn.jsdelivr.net/gh/fancyapps/fancybox@3/dist/jquery.fancybox.min.js"></script>
  <script src="//cdn.jsdelivr.net/npm/lozad@1/dist/lozad.min.js"></script>
  <script src="/lib/velocity/velocity.min.js"></script>
  <script src="/lib/velocity/velocity.ui.min.js"></script>

<script src="/js/utils.js"></script>

<script src="/js/motion.js"></script>


<script src="/js/schemes/pisces.js"></script>


<script src="/js/next-boot.js"></script>


  <script defer src="/lib/three/three.min.js"></script>


  



  <script>
  if (CONFIG.page.isPost) {
    wpac_init = window.wpac_init || [];
    wpac_init.push({
      widget: 'Rating',
      id    : 26171,
      el    : 'wpac-rating',
      color : 'fc6423'
    });
    (function() {
      if ('WIDGETPACK_LOADED' in window) return;
      WIDGETPACK_LOADED = true;
      var mc = document.createElement('script');
      mc.type = 'text/javascript';
      mc.async = true;
      mc.src = '//embed.widgetpack.com/widget.js';
      var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(mc, s.nextSibling);
    })();
  }
  </script>

  
<script src="/js/local-search.js"></script>













  

  


<script>
NexT.utils.loadComments(document.querySelector('#valine-comments'), () => {
  NexT.utils.getScript('//unpkg.com/valine/dist/Valine.min.js', () => {
    var GUEST = ['nick', 'mail'];
    var guest = 'nick,mail,link';
    guest = guest.split(',').filter(item => {
      return GUEST.includes(item);
    });
    new Valine({
      el         : '#valine-comments',
      verify     : false,
      notify     : false,
      appId      : 'puzQn5yHhkbtBj2VGFbqJ4FE-MdYXbMMI',
      appKey     : 'lmcgKM2surcxNxMRiqInHjkU',
      placeholder: "Just go go",
      avatar     : 'retro',
      meta       : guest,
      pageSize   : '10' || 10,
      visitor    : true,
      lang       : '' || 'zh-cn',
      path       : location.pathname,
      recordIP   : false,
      serverURLs : ''
    });
  }, window.Valine);
});
</script>
<div class="moon-menu">
  <div class="moon-menu-items">
    
    <div id="moon-menu-item-back2bottom" class="moon-menu-item" onclick="back2bottom()">
      <i class="fa fa-chevron-down"></i>    </div>
    
    <div id="moon-menu-item-back2top" class="moon-menu-item" onclick="back2top()">
      <i class="fa fa-chevron-up"></i>    </div>
    
  </div>
  <div class="moon-menu-button">
    <svg class="moon-menu-bg">
      <circle class="moon-menu-cricle" cx="50%" cy="50%" r="44%"/>
      <circle class="moon-menu-border" cx="50%" cy="50%" r="48%"/>
    </svg>
    <div class="moon-menu-content">
      <div class="moon-menu-icon"><i class="fas fa-ellipsis-v"></i></div>
      <div class="moon-menu-text"></div>
    </div>
  </div>
</div><script src="/js/injector.js"></script>
<!-- 数字雨 -->
<!-- <canvas id="canvas" width="1440" height="900" ></canvas> -->
<!-- <script type="text/javascript" src="/js/src/DigitalRain.js"></script> -->
<!-- 页面点击小红心 -->
<script type="text/javascript" src="/js/src/clicklove.js"></script>
<!--浏览器搞笑标题-->
<script type="text/javascript" src="/js/FunnyTitle.js"></script>
<!-- Live2D看板娘 -->
<!-- <script src="/live2d-widget/autoload.js"></script> -->
<!-- <script src="https://feirouafeirou.github.io/live2d-widget/autoload.js"></script> -->
<!-- 网页音乐播放器 -->
<!-- <link rel="stylesheet" href="/dist/APlayer.min.css">
<div id="aplayer"></div>
<script type="text/javascript" src="/dist/APlayer.min.js"></script>
<script type="text/javascript" src="/dist/music.js"></script> -->
</body>

</html>
